package com.wzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzz.entity.Class;
import com.wzz.entity.ClassStudent;
import com.wzz.entity.User;
import com.wzz.mapper.ClassMapper;
import com.wzz.mapper.ClassStudentMapper;
import com.wzz.mapper.UserMapper;
import com.wzz.service.ClassService;
import com.wzz.vo.PageResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    private final ClassMapper classMapper;
    private final ClassStudentMapper classStudentMapper;
    private final UserMapper userMapper;

    @Override
    @Transactional
    public void createClass(Class clazz) {
        clazz.setCreateTime(new Date());
        clazz.setStatus(1);
        classMapper.insert(clazz);
    }

    @Override
    @Transactional
    public void updateClass(Class clazz) {
        classMapper.updateById(clazz);
    }

    @Override
    @Transactional
    public void deleteClass(Integer id) {
        // 删除班级
        classMapper.deleteById(id);
        // 删除班级-学生关联
        QueryWrapper<ClassStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("class_id", id);
        classStudentMapper.delete(wrapper);
    }

    @Override
    public PageResponse<Class> getClassList(Integer pageNo, Integer pageSize, Integer teacherId) {
        IPage<Class> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        if (teacherId != null) {
            wrapper.eq("teacher_id", teacherId);
        }
        wrapper.orderByDesc("create_time");
        page = classMapper.selectPage(page, wrapper);
        
        return PageResponse.<Class>builder()
                .data(page.getRecords())
                .total(page.getTotal())
                .build();
    }

    @Override
    @Transactional
    public void addStudentToClass(Integer classId, Integer studentId) {

        Integer studentId1 = classStudentMapper.selectCount(new QueryWrapper<ClassStudent>().eq("student_id", studentId));
        if (studentId1 >0){
            throw new RuntimeException("该学生已加入班级");
        }
        ClassStudent classStudent = new ClassStudent();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", studentId));
        classStudent.setClassId(classId);
        classStudent.setStudentId(studentId);
        classStudent.setJoinTime(new Date());
        classStudent.setStatus(1);
        classStudent.setStudentName(user.getTrueName());
        classStudentMapper.insert(classStudent);
    }

    @Override
    @Transactional
    public void removeStudentFromClass(Integer classId, Integer studentId) {
        QueryWrapper<ClassStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("class_id", classId)
                .eq("student_id", studentId);
        classStudentMapper.delete(wrapper);
    }

    //根据学生id查班级信息
    @Override
    public Class UserFromClass(Integer id) {
        QueryWrapper<ClassStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", id);
        ClassStudent classStudent = classStudentMapper.selectOne(wrapper);
        QueryWrapper<Class> Wrapper = new QueryWrapper<>();
        Wrapper.eq("id", classStudent.getClassId());
        Class aClass = classMapper.selectOne(Wrapper);
        return aClass;
    }

    @Override
    public PageResponse<ClassStudent> getClassStudent(Integer pageNo, Integer pageSize, Integer classId) {
        IPage<ClassStudent> page = new Page<>(pageNo, pageSize);
        QueryWrapper<ClassStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("class_id", classId);
        page = classStudentMapper.selectPage(page, wrapper);
        return PageResponse.<ClassStudent>builder()
                .data(page.getRecords())
                .total(page.getTotal())
                .build();

    }
} 